package org.hackystat.devcathlon.ui.page.matches.manage.creatematchpanel;

import static org.junit.Assert.assertNotNull;
import javax.xml.datatype.XMLGregorianCalendar;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.hackystat.devcathlon.configuration.ConfigurationManager;
import org.hackystat.devcathlon.engine.event.impl.BuildBeforeCommit;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.hackystat.devcathlon.ui.DevcathlonApplication;
import org.hackystat.devcathlon.ui.page.matches.manage.MatchesManagePage;
import org.hackystat.utilities.tstamp.Tstamp;
import org.junit.Before;
import org.junit.Test;

/**
 * Test the create match panel.
 * 
 * @author robin
 *
 */
public class TestCreateMatchPanel extends DevcathlonTestHelper {

  /** The actual build and commit interval. */
  private int buildCommitInterval = 10;
  /** The actual commit streak threshold. */
  private int streakThreshold = 10;
  /** The actual commit streak threshold. */
  private int bonusWeight = 10;

  private String teamName1 = "TestTeam1";
  private String teamOwner1 = "admin";
  private String member1a = "Member1a";
  private String member1b = "Member1b";
  private String teamName2 = "TestTeam2";
  private String teamOwner2 = "TeamOwner2";
  private String member2a = "Member2a";
  private String member2b = "Member2b";
  /** Instance of an team. */
  Team team1;
  /** Instance of an team. */
  Team team2;
  /** Sensor base host URL. */
  private String sensorBaseHost = "http://localhost:9976/sensorbase/";
  /** Daily project data host URL. */
  private String dailyProjectDataHost = "http://localhost:9977/dailyprojectdata/";
  /** Instance of DevcathlonApplication. */
  private DevcathlonApplication devcathlonApplication;
  /** Instance of WicketTester. */
  private WicketTester tester;
  String location = "org.hackystat.devcathlon.engine.event.impl.BuildBeforeCommit";
  /**
   * Sets up a Devcathlon Configuration for testing.
   * 
   * @throws Exception If there are any errors.
   */
  @Before
  public void setUp() throws Exception {
    String eventName = BuildBeforeCommit.eventName;
    ConfigurationManager configManager = ConfigurationManager.getInstance();
    configManager.setBaseScoreWeight(eventName, 10);
    configManager.getConfiguration(eventName);
    configManager.setWakeupInterval(eventName, 60);
    configManager.setEventLocation(eventName, location);
    configManager.addProperty(eventName, BuildBeforeCommit.buildCommitKey, String
        .valueOf(buildCommitInterval));
    configManager.addProperty(eventName, BuildBeforeCommit.upperThresholdKey, String
        .valueOf(streakThreshold));
    configManager.addProperty(eventName, BuildBeforeCommit.meetThresholdKey, String
        .valueOf(bonusWeight));

    // Create the teams.
    team1 = makeTeamAndInvites(teamName1, teamOwner1, member1a, member1b);
    TeamManager.getInstance().getTeam(teamName1); //.setProjectName(teamName1);
    team2 = makeTeamAndInvites(teamName2, teamOwner2, member2a, member2b);
    TeamManager.getInstance().getTeam(teamName2); //.setProjectName(teamName2);
    team1.getInvited();
    team2.getInvited();
    devcathlonApplication = new DevcathlonApplication(dailyProjectDataHost, sensorBaseHost);
    tester = new WicketTester(devcathlonApplication);
  }
  
  /**
   * Test me out.
   * @throws Exception If there are any errors.
   */
  @Test
  public void testCreateMatchPanel() throws Exception {
    tester.setupRequestAndResponse();
    tester.startPage(devcathlonApplication.getHomePage());
    tester.assertRenderedPage(devcathlonApplication.getHomePage());

    FormTester formTester = tester.newFormTester("loginForm");
    formTester.setValue("user", "admin@hackystat.org");
    formTester.setValue("password", "admin@hackystat.org");
    formTester.submit();
    
    tester.startPage(CreateNewMatchPanel.class);
    tester.assertRenderedPage(CreateNewMatchPanel.class);
    
    assertNotNull("wicket form not found", tester.getTagByWicketId("createMatchForm"));
    
    FormTester createForm = tester.newFormTester("createMatchForm");
    XMLGregorianCalendar now = Tstamp.makeTimestamp();
    now = Tstamp.incrementDays(now, 1);
    XMLGregorianCalendar end = Tstamp.incrementDays(now, 5);
    
    createForm.setValue("matchStartTimeBorder:startTimeInput", convertDateToString(now));
    createForm.setValue("matchEndTimeBorder:endTimeInput", convertDateToString(end));
    createForm.setValue("matchNameBorder:matchName", "JUnit Test");
    createForm.select("teamNameBorder:teamName", 0);
    createForm.select("opponentNameBorder:opponentName", 0);
    createForm.selectMultiple("eventListBorder:eventTypes", new int[] {0});
    createForm.submit("submitButton");
    tester.assertRenderedPage(MatchesManagePage.class);
  }
  
  /**
   * Reformat the input date to a string.
   * @param date The xml input date.
   * @return The string date.
   */
  public String convertDateToString (XMLGregorianCalendar date) {
    String year = date.toString().substring(0, 4);
    String month = date.toString().substring(5, 7);
    String day = date.toString().substring(8, 10);
    return month + "/" + day + "/" + year;
  }

}
